package com.android.bookgarden.ui;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.bookgarden.MainActivity;
import com.android.bookgarden.R;
import com.android.bookgarden.adapter.AvocationAdapter;
import com.android.bookgarden.adapter.MyXqAdapter;
import com.android.bookgarden.adapter.SearchAdapter;
import com.android.bookgarden.base.BaseActivity;
import com.android.bookgarden.bean.BtItem;
import com.android.bookgarden.bean.SearchBean;
import com.android.bookgarden.db.XutilsDataDao;
import com.android.bookgarden.recycleview.BaseViewHolder;
import com.android.bookgarden.tabfragment.TestIndexFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 我的兴趣
 * Create by zouqh
 * on 2018/12/1
 **/
public class MyInterestActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.close)
    ImageView close;
    @BindView(R.id.title_text)
    TextView titleText;
    @BindView(R.id.ritgh_icon)
    ImageView ritghIcon;
    @BindView(R.id.ritgh_text)
    TextView ritghText;
    @BindView(R.id.editText)
    TextView editText;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.recycler2)
    RecyclerView recycler2;

    private List<BtItem> list1;
    private MyXqAdapter adapter2;
    private List<BtItem> list2;
    private List<BtItem> btItems;
    private AvocationAdapter avocationAdapter;
    private boolean flag;
    private XutilsDataDao dataDao;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_interest;
    }

    @Override
    protected void initView() {
        dataDao = XutilsDataDao.getInstance(this);
        btItems = dataDao.queryAll(BtItem.class);
        titleText.setText("我的兴趣");
        close.setOnClickListener(this);
        editText.setOnClickListener(this);
        list1 = new ArrayList<>();
        list2 = new ArrayList<>();
        if (null==btItems||btItems.size()<=0){
            finish();
            return;

        }
        System.out.println(btItems);
        for (int i = 0; i < btItems.size() ; i++) {
            BtItem item =btItems.get(i);
            if (btItems.get(i).isShow()){
                list1.add(item);
            }else{
                list2.add(item);
            }
        }

        avocationAdapter = new AvocationAdapter(list1,this);
        adapter2 = new MyXqAdapter(list2,this);
        recycler.setLayoutManager(new GridLayoutManager(this,4));
        recycler.setAdapter(avocationAdapter);
        recycler2.setLayoutManager(new GridLayoutManager(this,4));
        recycler2.setAdapter(adapter2);
        avocationAdapter.setOnItemClickLitener(new AvocationAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, BaseViewHolder viewHolder) {
                BtItem bs = list1.get(position);
                bs.setShow(false);
                list2.add(bs);
                list1.remove(position);
                avocationAdapter.notifyDataSetChanged();
                adapter2.notifyDataSetChanged();
            }
        });

        adapter2.setOnItemClickLitener(new MyXqAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, BaseViewHolder viewHolder) {
                BtItem bs = list2.get(position);
                bs.setShow(true);
                list1.add(bs);
                list2.remove(position);
                avocationAdapter.notifyDataSetChanged();
                adapter2.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onRequestPermissions(String str, boolean z) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.close:
                finish();
                break;

            case R.id.editText:
                if (flag){
                    flag = false;
                    editText.setText("编辑");
                    updateData();
                    TestIndexFragment.testIndexFragment.init();
                }else {
                    flag = true;
                    editText.setText("完成");
                }
                avocationAdapter.setDelete(flag);
                break;
        }
    }

    private void updateData(){
        for (int i = 0; i <list1.size() ; i++) {
            dataDao.update(list1.get(i));
        }

        for (int i = 0; i <list2.size() ; i++) {
            dataDao.update(list2.get(i));
        }
    }

}
